//
//  SISSHouseEnrollFirstViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/7/27.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSHouseEnrollFirstViewController.h"
#import "SWForm.h"
#import "SISSFilterChooseBaseViewController.h"
#import "SISSRoomChooseViewController.h"
#import "SISSHouseEnrollSecondViewController.h"
#import "SISSJsonRequestNetworkingManager+Registration.h"
#import "SISSRoomNumberModel.h"

#import "SISSHouseResourceEnrollViewController.h"
#import "SISSEnrollPropertyCheckViewController.h"
#import "SISSCheckHouseResultModel.h"

@interface SISSHouseEnrollFirstViewController ()<UIActionSheetDelegate>

@property (weak, nonatomic) SWFormItem *communityItem;
@property (weak, nonatomic) SWFormItem *buildingItem;
@property (weak, nonatomic) SWFormItem *roomItem;

/// 栋列表
@property (strong, nonatomic) NSArray<SISSBuildingNumberModel *> *buildingArr;
/// 选中的栋modl
@property (strong, nonatomic) SISSBuildingNumberModel *buildingNumberModel;
/// 选中的房号model
@property (strong, nonatomic) SISSRoomNumberModel *roomNumberModel;

@end

@implementation SISSHouseEnrollFirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];

//    self.title = @"房源登记";
    // 添加左侧的关闭按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    [self initUI];
    
    if (self.checkHouseModel) {
        
        self.communityItem.info = self.checkHouseModel.house_name;
        self.buildingItem.info = self.checkHouseModel.house_build_no;
        self.roomItem.info = self.checkHouseModel.house_room_no;
    }
}

- (void)back {
    
    if (self.isRentOut) {
        [self dismissViewControllerAnimated:YES completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
    } else {
//        [self dismissViewControllerAnimated:YES completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }

}

#pragma mark - UI
- (void)initUI {
    NSMutableArray *items = [NSMutableArray array];
    
    SWFormItem *communityItem = [SWFormItem sw_itemWithTitle:@"小区" info:@"" itemType:SWFormItemTypeSelect editable:NO required:YES keyboardType:UIKeyboardTypeDefault];
    communityItem.itemSelectCompletion = ^(SWFormItem *item) {
        [self selectCommunityWithItem:item];
    };
    [items addObject:communityItem];
    self.communityItem = communityItem;

    SWFormItem *buildingItem = [SWFormItem sw_itemWithTitle:@"楼栋" info:@"" itemType:SWFormItemTypeSelect editable:NO required:YES keyboardType:UIKeyboardTypeDefault];
    buildingItem.itemSelectCompletion = ^(SWFormItem *item) {
        [self selectBuildingWithItem:item];
    };
    [items addObject:buildingItem];
    self.buildingItem = buildingItem;


    SWFormItem *roomItem = [SWFormItem sw_itemWithTitle:@"房号" info:@"" itemType:SWFormItemTypeSelect editable:NO required:YES keyboardType:UIKeyboardTypeDefault];
    roomItem.itemSelectCompletion = ^(SWFormItem *item) {
        [self selectRoomWithItem:item];
    };
//    room.unit = @"";
    [items addObject:roomItem];
    self.roomItem = roomItem;
    
    
    SWFormSectionItem *sectionItem = [SWFormSectionItem sw_sectionItem:items];
    //    sectionItem.headerHeight = 10;
    [self.mutableItems addObject:sectionItem];
    
    self.formTableView.tableFooterView = [self footerView];
}

- (void)selectCommunityWithItem:(SWFormItem *)item {
    if (self.checkHouseModel.house_name && self.checkHouseModel.house_name.length > 0) return;
    SISSFilterChooseBaseViewController *vc = [[SISSFilterChooseBaseViewController alloc] init];
    vc.title = @"小区选择";
    vc.placeholder = @"请输入小区名称";
    vc.selectBlock = ^(NSString *tiltle) {
//        if (![tiltle isEqualToString:self.communityItem.info]) {
//            if (!self.checkHouseModel.house_build_no) self.buildingItem.info = @"";
//            if (!self.checkHouseModel.house_room_no) self.roomItem.info = @"";
//        }
        self.communityItem.info = tiltle;
        
        [kJsonRequestNetworkingManager requestBuildingResourceWithCommunityName:tiltle successBlock:^(NSArray<SISSBuildingNumberModel *> *buildingArr) {
            
            self.buildingArr = buildingArr;
            
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
        [self.formTableView reloadData];
    };
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)selectBuildingWithItem:(SWFormItem *)item {
    if (self.checkHouseModel.house_build_no && self.checkHouseModel.house_build_no.length > 0) return;
    if (self.communityItem.info.length <= 0) {
        [self alertActionAlertWithTitle:@"提示" message:@"请先选择小区信息"];
        return;
    }
    
    SISSFilterChooseBaseViewController *vc = [[SISSFilterChooseBaseViewController alloc] init];
    vc.isBuildingIn = YES;
    vc.title = @"栋号选择";
    vc.dataSource = self.buildingArr;
    vc.placeholder = @"未找到您的楼栋，请输入栋号";
    vc.selectBuildingBlock = ^(SISSBuildingNumberModel *model) {
//        if (![model.bnum isEqualToString:self.buildingItem.info] && !self.checkHouseModel.house_room_no) {
//            self.roomItem.info = @"";
//        }
        self.buildingItem.info = model.bnum;
        self.buildingNumberModel = model;
        [self.formTableView reloadData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)selectRoomWithItem:(SWFormItem *)item {
    if (self.checkHouseModel.house_room_no && self.checkHouseModel.house_room_no.length > 0) return;
    if (self.communityItem.info.length <= 0) {
        [self alertActionAlertWithTitle:@"提示" message:@"请先选择小区信息"];
        return;
    }
    
    if (self.buildingItem.info.length <= 0) {
        [self alertActionAlertWithTitle:@"提示" message:@"请先选择楼栋信息"];
        return;
    }
    
    SISSRoomChooseViewController *vc = [[SISSRoomChooseViewController alloc] init];
    vc.title = @"房号选择";
    vc.buildingId = self.buildingNumberModel.dongId;
//    vc.buildingId = @"7708";
    vc.selectBlock = ^(SISSRoomNumberModel *roomNumberModel) {
        self.roomNumberModel = roomNumberModel;
        self.roomItem.info = roomNumberModel.dnum;
        [self.formTableView reloadData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

/**
 下一步按钮点击事件
 */
- (void)nextStepButtonClickAction {
    // 这里只是简单描述校验逻辑，可根据自身需求封装数据校验逻辑
    //    SISSHouseEnrollSecondViewController *vc = [[SISSHouseEnrollSecondViewController alloc] init];
    //    [self.navigationController pushViewController:vc animated:YES];
    
    [SWFormHandler jyt_checkFormNullDataWithWithDatas:self.mutableItems success:^{
        
        NSLog(@"community === %@", self.communityItem.info);
        //NSLog(@"images === %@", image.images);
        NSLog(@"building === %@", self.buildingItem.info);
        NSLog(@"room === %@", self.roomItem.info);
        if (self.isRentOut) {
            SISSEnrollPropertyCheckViewController *vc = [[SISSEnrollPropertyCheckViewController alloc] init];
            SISSRentOutModel *rentOutModel = [[SISSRentOutModel alloc] init];
            rentOutModel.neighbourhood = self.communityItem.info;
            rentOutModel.bnum = self.buildingItem.info;
            rentOutModel.dnum = self.roomItem.info;
            rentOutModel.landId = self.roomNumberModel.lanId;
            vc.rentOutModel = rentOutModel;
            vc.isRegistration = self.isRegistration;
            [self.navigationController pushViewController:vc animated:YES];
        } else {
//            SISSHouseEnrollSecondViewController *vc = [[SISSHouseEnrollSecondViewController alloc] init];
            SISSHouseResourceEnrollViewController *vc = [[SISSHouseResourceEnrollViewController alloc] init];
            SISSHouseResourceModel *houseModel = [[SISSHouseResourceModel alloc] init];
            houseModel.neighbourhood = self.communityItem.info;
            houseModel.bnum = self.buildingItem.info;
            houseModel.dnum = self.roomItem.info;
            houseModel.landId = self.roomNumberModel.lanId;
            houseModel.houseInfoNo = self.checkHouseModel.verify_code;
            houseModel.certNo = self.checkHouseModel.cert_no;
            vc.landId = self.roomNumberModel.lanId;
            vc.houseModel = houseModel;
            vc.isRegistration = self.isRegistration;
            vc.isCooperation = self.isCooperation;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        
    } failure:^(NSString *error) {
        NSLog(@"error====%@",error);
        [self alertActionAlertWithTitle:@"提示" message:error];
    }];
}

/**
 创建footer
 */
- (UIView *)footerView {
    UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 80)];
    
    UIButton *nextStepButton = [UIButton buttonWithType:UIButtonTypeSystem];
    nextStepButton.bounds = CGRectMake(0, 0, SCREEN_WIDTH * 0.8, 40);
    nextStepButton.center = footer.center;
    nextStepButton.backgroundColor = MAIN_COLOR;
    nextStepButton.layer.cornerRadius = 8;
    nextStepButton.layer.masksToBounds = YES;
    [nextStepButton setTitle:@"下一步" forState:UIControlStateNormal];
    [nextStepButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [nextStepButton addTarget:self action:@selector(nextStepButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    [footer addSubview:nextStepButton];
    
    return footer;
}

#pragma mark - 懒加载
- (NSArray<SISSBuildingNumberModel *> *)buildingArr {
    if (!_buildingArr) {
        _buildingArr = [NSArray array];
    }
    return _buildingArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
